<?php
namespace Admin\Controller;
use Think\Controller;
class UserwpController extends CommonController {
    public function index()
    {
        $start = strtotime(I('post.start'));
        $stop = strtotime(I('post.stop'));
        $phone = I('post.phone');

        if (empty($start) && empty($stop) && !empty($phone)) {
            //为手机
            if (is_numeric($phone)) {
                $userwps = M('gy_wuping')->where(['phone'=>$phone])->select();
                //数量
                $count = M('gy_wuping')->where(['phone'=>$phone])->count();
            }else {
                //根据项目名称换id
                $xm_id = M('gy_xm')->where(['xm_name'=>$phone])->getField('id');
                $userwps = M('gy_wuping')->where(['xm_id'=>$xm_id])->select();
                //数量
                $count = M('gy_wuping')->where(['xm_id'=>$xm_id])->count();
            }

        }elseif (!empty($start) && !empty($stop) && empty($phone)) {
            $map['time'] = array(array('gt',$start),array('lt',$stop)) ;
            $userwps = M('gy_wuping')->where($map)->select();
            //数量
            $count = M('gy_wuping')->where($map)->count();

        }elseif (!empty($start) && !empty($stop) && !empty($phone)) {
            if (is_numeric($phone)) {
                $map['time'] = array(array('gt',$start),array('lt',$stop)) ;
                $userwps = M('gy_wuping')->where($map)->where(['phone'=>$phone])->select();
                //数量
                $count = M('gy_wuping')->where($map)->where(['phone'=>$phone])->count();
            }else {
                //根据项目名称换id
                $xm_id = M('gy_xm')->where(['xm_name'=>$phone])->getField('id');
                $map['time'] = array(array('gt',$start),array('lt',$stop)) ;
                $userwps = M('gy_wuping')->where($map)->where(['xm_id'=>$xm_id])->select();
                //数量
                $count = M('gy_wuping')->where($map)->where(['xm_id'=>$xm_id])->count();
            }

    	}else {
            //分页显示
            $gy_wuping = M('gy_wuping'); // 实例化User对象
            $count      = $gy_wuping->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $userwps = $gy_wuping->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            $this->assign('page',$show);// 赋值分页输出
		}
        $this->assign('count',$count);
		$this->assign('userwps',$userwps);
        $this->display();
    }

    /**
     * 导出 匿名家长需求表所有数据
     */
    public function gy_wuping_export(){
        $nm_list = M('gy_wuping')->field('id,xm_id,fq_id,uid,name,phone,wp_name,num,is_fanshi,address,from_unixtime(time),qx')->select();
        $subject="Excel导出-公益用户物品表";
        $title=array("id","项目ID","发起ID","发起人ID","联系人","手机","捐赠品","捐赠数量","发起方式","收书地址","捐赠时间","赠品去向动态");
        exportExcel($nm_list,$title,$subject); 
    }

    //编辑
    public function edit()
    {   
    	if (IS_POST) {
    		$id = I('post.id');

            $data['is_fanshi'] = I('post.is_fanshi');
            $data['address'] = I('post.address');

            $data['wp_name'] = I('post.wp_name');
            $data['num'] = I('post.num');
    		$data['qx'] = I('post.sign');

            if ($data['is_fanshi'] == 1) {
                if (empty($data['address'])) {
                    $arr = array(
                        'error' => 0 ,
                        'info' => '请填写捐赠地址'
                        );
                    echo json_encode($arr);
                    exit;
                }
            }else {
                $data['address'] = '';
            }

            if (!ctype_digit($data['num'])) {
                $arr = array(
                    'error' => 0 ,
                    'info' => '捐赠数量请填写整数'
                    );
                echo json_encode($arr);
                exit;
            }
            if (empty($data['wp_name'])) {
                $arr = array(
                    'error' => 0 ,
                    'info' => '请填写捐赠物品'
                    );
                echo json_encode($arr);
                exit;
            }
    		if (empty($data['qx'])) {
    			$arr = array(
                    'error' => 0 ,
                    'info' => '请填写赠品去向动态'
                    );
                echo json_encode($arr);
                exit;
    		}
            //获取物品信息
            $wps = M('gy_wuping')->where(['id'=>$id])->find();
            $_SESSION['oldnum'] = $wps['num'];
    		$res = M('gy_wuping')->where(array('id'=>$id))->setField($data);
    		if ($res) {
                //减去此物品old数量
                M('gy_xm')->where(array('id'=>$wps['xm_id']))->setDec('xm_num',$_SESSION['oldnum']);
                //编辑成功 xm_num 自动加上new数量
                M('gy_xm')->where(array('id'=>$wps['xm_id']))->setInc('xm_num',$data['num']);
                //fq_id存在 fq_num 自动加上数量
                if ($wps['fq_id'] != 0) {
                    //减去old 加new
                    M('gy_faqi')->where(array('id'=>$wps['fq_id']))->setDec('fq_num',$_SESSION['oldnum']);
                    M('gy_faqi')->where(array('id'=>$wps['fq_id']))->setInc('fq_num',$data['num']);
                }
                //删除oldnum
                unset($_SESSION['oldnum']);

    			$arr = array(
                    'error' => 1 ,
                    'info' => '更新成功'
                    );
                echo json_encode($arr);
    		}else {
    			$arr = array(
                    'error' => 0 ,
                    'info' => '更新失败'
                    );
                echo json_encode($arr);
    		}

    	}else {
	    	$id = I('get.id');
	    	$userwp_info = M('gy_wuping')->where(['id'=>$id])->find();
	    	$this->assign('userwp_info',$userwp_info);
	    	$this->display();
    	}
    }

    //删除
    public function delete()
    {
    	if (IS_POST) {
    		$id = I('post.id');
            //获取物品信息
            $wps = M('gy_wuping')->where(['id'=>$id])->find();
            $_SESSION['oldnum'] = $wps['num'];

    		$res = M('gy_wuping')->delete($id);
    		if ($res) {
                //减去此物品old数量
                M('gy_xm')->where(array('id'=>$wps['xm_id']))->setDec('xm_num',$_SESSION['oldnum']);
                //fq_id存在
                if ($wps['fq_id'] != 0) {
                    //减去old
                    M('gy_faqi')->where(array('id'=>$wps['fq_id']))->setDec('fq_num',$_SESSION['oldnum']);
                }
                //删除oldnum
                unset($_SESSION['oldnum']);

    			$arr = array(
                    'error' => 1 ,
                    'info' => '删除成功'
                    );
                echo json_encode($arr);
    		}else {
    			$arr = array(
                    'error' => 0 ,
                    'info' => '删除失败'
                    );
                echo json_encode($arr);
    		}
    	}
    }

}